package com.lin.service;

import com.lin.mapper.DirDistrictMapper;
import com.lin.remote.MapDirectionClient;
import com.lin.remote.MapDistrictClient;
import com.lzl.constant.AmapConfigConstants;
import com.lzl.constant.CommonStatusEnum;
import com.lzl.dto.DicDistrict;
import com.lzl.responese.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * //TODO
 *
 * @author zhaolinlin
 * @description //TODO
 * @createTime 2022/8/25 11:40
 */
@Service
@Slf4j
public class DistrictService {
    @Autowired
    MapDistrictClient mapDistrictClient;

    @Autowired
    DirDistrictMapper dirDistrictMapper;

    public ResponseResult insertByDistrict(String keywords){
        //获取区域信息
        String district = mapDistrictClient.district(keywords);
        //解析区域json格式
        JSONObject jsonObject = JSONObject.fromObject(district);
        int status = jsonObject.getInt(AmapConfigConstants.STATUS);
        if(status !=1){
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        //status == 1
        JSONArray districtArray = jsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        //递归获取下一级的信息
        recursionSubJSON(districtArray,jsonObject);
        return ResponseResult.success();

    }

    public void recursionSubJSON(JSONArray districtArray, JSONObject jsonObject){
        if(districtArray ==null || districtArray.size() == 0 ){
            return ;
        }

        for(int i=0;i<districtArray.size();i++){
            DicDistrict dicDistrict = new DicDistrict();
            if(jsonObject.has(AmapConfigConstants.ADCODE)){
                String code = jsonObject.getString(AmapConfigConstants.ADCODE);
                dicDistrict.setParentAddressCode(code);
            }
            JSONObject indexDistrict = districtArray.getJSONObject(i);
            String adcode = indexDistrict.getString(AmapConfigConstants.ADCODE);
            String name = indexDistrict.getString(AmapConfigConstants.NAME);
            String level = indexDistrict.getString(AmapConfigConstants.LEVEL);

//            private String addressCode;
//            private String addressName;
//            private String parentAddressCode;
//            private Byte level;
            dicDistrict.setAddressCode(adcode);
            dicDistrict.setAddressName(name);
            if(!jsonObject.has(AmapConfigConstants.ADCODE)){
                dicDistrict.setParentAddressCode("0");
                dicDistrict.setLevel((byte)0);
            }else {
                switch(level){
                    case "province":
                        dicDistrict.setLevel((byte)1);
                        break;
                    case "city" :
                        dicDistrict.setLevel((byte)2);
                        break;
                    case "district" :
                        dicDistrict.setLevel((byte)3);
                        break;

                }
            }

            if(level.equals(AmapConfigConstants.STREET)  ){
                continue;
            }
            dirDistrictMapper.insert(dicDistrict);
            JSONArray nextJSONArray = indexDistrict.getJSONArray(AmapConfigConstants.DISTRICTS);
            recursionSubJSON(nextJSONArray,indexDistrict);
        }
    }
}
